package com.hunlian.makelove.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hunlian.makelove.R;

/**
 * （网络）异常重试页面
 * @author Jiangshunbin
 *
 */
public class NetExceptionActivity extends BaseActivity {
	private boolean mShowTitle = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_net_exception);
		
		RelativeLayout layTitle = (RelativeLayout)findViewById(R.id.ll_title);
		TextView txtTitle = (TextView)findViewById(R.id.title);
		mShowTitle = getIntent().getBooleanExtra("show_title", false);
		String title = getIntent().getStringExtra("title_text");
		txtTitle.setText(title);
		if(!mShowTitle){
			layTitle.setVisibility(View.GONE);
		}
		
		ImageButton btnBack = (ImageButton)findViewById(R.id.back);
		btnBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				NetExceptionActivity.this.finish();
			}
		});
		
		Button btnRefresh = (Button)findViewById(R.id.btnRefresh);
		btnRefresh.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				setResult(RESULT_OK);
				NetExceptionActivity.this.finish();
			}
		});
	}
	
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (event.getAction() == KeyEvent.ACTION_DOWN) {
			switch (event.getKeyCode()) {
			case KeyEvent.KEYCODE_BACK:
				//如果不显示title的话，表示不显示的“返回”按钮不能用，所以不支持关闭页面返回
				if(!mShowTitle){
					return true;
				} 
				break;
			}
		}

		return super.dispatchKeyEvent(event);
	}
	
	public static void startNetExceptionActivity(Activity activity, int requestCode, 
			boolean showTitle, String titleText){
		Intent intent = new Intent(activity, NetExceptionActivity.class);
		intent.putExtra("title_text", titleText);
		intent.putExtra("show_title", showTitle);
		activity.startActivityForResult(intent, requestCode);
	}
	
	public static void startNetExceptionActivity(Activity activity, int requestCode){
		startNetExceptionActivity(activity, requestCode, false, "");
	}


}
